#include "ProtocolDialog.h"
#include "ui_ProtocolDialog.h"
#include <QFileInfoList>
#include <QDir>
#include <QDebug>
#include <QMessageBox>

ProtocolDialog::ProtocolDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProtocolDialog)
{
    ui->setupUi(this);
    setWindowTitle("选择驱动协议");
    ui->treeWidget->setHeaderHidden(true);
    ui->treeWidget->setIconSize(QSize(20,20));
    m_type = Protocal_Type::Collect_Type;
   // ui->treeWidget->setsi
//    getDirectName(QDir::currentPath()+"/Collect/");
}

ProtocolDialog::~ProtocolDialog()
{
    delete ui;
}

void ProtocolDialog::SetProtocalType(ProtocolDialog::Protocal_Type type)
{
    m_type = type;
    if(type == Protocal_Type::Collect_Type)
    {
        getDirectName(QDir::currentPath()+"/Collect/");
    }else
    {
        getDirectName(QDir::currentPath()+"/Update/");
    }
}

QString ProtocolDialog::GetSelectProtoCate()
{
    return m_protoName;
}

QString ProtocolDialog::GetSelectProtoName()
{
    return m_protoCate;
}
//遍历collect下面的所有子文件夹名称
void ProtocolDialog::getDirectName(QString strDirect)
{
    qDebug()<<strDirect;
    QDir dir(strDirect);
    dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
    QStringList folderList = dir.entryList();

    foreach (const QString &folderName, folderList) {
        qDebug() << "Subfolder Name:" << folderName;
        parseXmlFile(strDirect+folderName);
    }
     QMap<QString,QVector<QString>>::iterator  itBegin = m_mapCategory.begin();
     while(itBegin != m_mapCategory.end())
     {
         QTreeWidgetItem * rootItem = new QTreeWidgetItem(ui->treeWidget);
         rootItem->setIcon(0,QIcon(":/resource/direct.png"));
         rootItem->setText(0,itBegin.key());
         foreach (QString item, itBegin.value()) {
             QTreeWidgetItem * subItem = new QTreeWidgetItem(rootItem);
             subItem->setIcon(0,QIcon(":/resource/dev.png"));
             subItem->setText(0,item);
             subItem->setData(0,Qt::UserRole,1);
         }
        itBegin++;
     }
//    m_projectItem = new QTreeWidgetItem(this);
//    QIcon icon1;
//    icon1.addPixmap(QPixmap(":/resource/Project.png"), QIcon::Normal);
//    m_projectItem->setData(0,Qt::UserRole,Node_Project);
//    m_projectItem->setIcon(0,icon1);
//    m_projectItem->setText(0,strName);
    //ui->treeWidget->add
}
//解析所有子文件夹的xml配置文件
void ProtocolDialog::parseXmlFile(QString fileDirect)
{
    QString xmlName = fileDirect + "/protocal.xml";
    xml_document    m_doc;				// doc
    if(!m_doc.load_file(xmlName.toLocal8Bit().data()))
    {
        qDebug()<<"open file failed :" << xmlName;
        return ;
    }
    xml_node cur = m_doc.root().child("protocal");
    if (cur == nullptr)
    {
        qDebug()<<"load file failed : no file SCL node :" << xmlName;
        return ;
    }

    qDebug()<<"cur name = "<<cur.name();
    ProtocalInfo  protocalInfo;
    protocalInfo.categoryName = cur.attribute("cate").value();
    protocalInfo.protoName = cur.attribute("name").value();
    protocalInfo.desc = cur.attribute("desc").value();
    protocalInfo.factoryName = cur.attribute("fact").value();
    if(m_mapCategory.contains(protocalInfo.categoryName))
    {
        m_mapCategory.find(protocalInfo.categoryName)->push_back(protocalInfo.protoName);
    }else
    {
        QVector<QString> vec;
        vec.push_back(protocalInfo.protoName);
        m_mapCategory.insert(protocalInfo.categoryName,vec);
    }
    m_mapProtocal.insert(protocalInfo.protoName,protocalInfo);
    m_doc.reset();
}

void ProtocolDialog::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    int userData = item->data(0,Qt::UserRole).toInt();
    if(userData == 1)
    {
        if(m_mapProtocal.contains(item->text(0)))
        {
            ProtocalInfo info = m_mapProtocal.find(item->text(0)).value();
            ui->lbCategory->setText( info.categoryName);
            ui->lineEdit->setText(info.protoName);
            ui->lineEdit_2->setText(info.factoryName);
        }
    }
}


void ProtocolDialog::on_pushButton_clicked()
{
   QTreeWidgetItem * item =  ui->treeWidget->currentItem();
   if(!item || item->data(0,Qt::UserRole) != 1)
   {
       QMessageBox::information(nullptr,"协议选择","未选中协议");
   }else
   {
       m_protoCate = ui->lineEdit_2->text();
       m_protoName = ui->lineEdit->text();
       accept();
   }
}


void ProtocolDialog::on_pushButton_2_clicked()
{
   QDialog::close();
}

